package utils;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.Vector;

/**
 *
 * @author Kvest
 */
public class XMLParser
{
    public static Vector getTags(String xml,String name)
    {
        String tmp,tag;
        tag = "<" + name;// + ">";
        Vector result = new Vector();
        int index = 0,indexEnd;
        while ((index = xml.indexOf(tag,index)) != -1)
        {
            indexEnd = xml.indexOf("/" + name + ">",index)  + 2 + name.length();
            try
            {
                tmp = xml.substring(index, indexEnd);
                result.addElement(tmp);
            }catch(Exception e){}
            //Смещаем индекс
            index++;
        }

        return result;
    }
    
    public static Vector getList(String xml,String name)
    {
        String tmp;
        name = "<" + name + " ";
        Vector result = new Vector();
        int index = 0,indexEnd;
        while ((index = xml.indexOf(name,index)) != -1)
        {
            indexEnd = xml.indexOf("/>",index)  + 2;
            try
            {
                tmp = xml.substring(index, indexEnd);
                result.addElement(tmp);
            }catch(Exception e){}
            //Смещаем индекс
            index++;
        }

        return result;
    }

    public static String getElement(String xml,String name)
    {
        int index = xml.indexOf("<" + name);
        if (index == -1)
        {
            return "";
        }

        int indexEnd = xml.indexOf("/" + name + ">",index)  + 2 + name.length();

         try
         {
            String tmp = xml.substring(index, indexEnd);
            return tmp;
         }catch(Exception e){return "";}
    }

    public static String getStringElement(String xml,String name) {
        return getStringElement(xml, name, "");
    }
    
    public static String getStringElement(String xml,String name, String defaultValue)
    {
        int index = xml.indexOf("<" + name);
        if (index == -1)
        {
            return defaultValue;
        }

        index = xml.indexOf(">", index) + 1;
        int indexEnd = xml.indexOf("<", index);
        try
        {
            return xml.substring(index, indexEnd);
        }catch (Exception e) {return defaultValue;}
    }

    public static int getIntElement(String xml,String name) {
        return getIntElement(xml, name, -1);
    }
    
    public static int getIntElement(String xml,String name, int defaultValue)
    {
        int index = xml.indexOf("<" + name);
        if (index == -1)
        {
            return defaultValue;
        }

        index = xml.indexOf(">", index) + 1;
        int indexEnd = xml.indexOf("<", index);    
        try
        {
            String codeStr = xml.substring(index, indexEnd);
            return Integer.parseInt(codeStr);
        }catch (Exception e) {return defaultValue;}
    }

    public static String getParamString(String xml,String name)
    {
        int index = xml.indexOf(" " + name + "=");
        if (index == -1)
        {
            return "";
        }

        index = xml.indexOf("\"", index) + 1;
        int indexEnd = xml.indexOf("\"", index);
        try
        {
            return xml.substring(index, indexEnd);
        }catch (Exception e) {return "";}
    }

    public static int getParamInt(String xml,String name)
    {
        //Получаем строковое значение
        String tmp = getParamString(xml, name);

        //Преобразовываем
        try
        {
            return Integer.parseInt(tmp);
        }catch(Exception e){return -1;}
    }

    public static float getParamFloat(String xml,String name)
    {
        //Получаем строковое значение
        String tmp = getParamString(xml, name);
        
        //Преобразовываем
         try
        {
            return Float.parseFloat(tmp);
        }catch(Exception e){return -1;}
    }

    public static String getExtra(String xml, String name)
    {
        int index = xml.indexOf("<extra name=\"" + name);
        if (index == -1)
        {
            return "";
        }

        index = xml.indexOf(">",index) + 1;
        int indexEnd = xml.indexOf("<", index);
        try
        {
            return xml.substring(index, indexEnd);
        }catch (Exception e) {return "";}
    }
}
